package com.xiaoyu.net.queue;

import android.content.Context;

import com.birbit.android.jobqueue.Job;
import com.birbit.android.jobqueue.JobManager;
import com.birbit.android.jobqueue.Params;
import com.birbit.android.jobqueue.config.Configuration;
import com.birbit.android.jobqueue.log.CustomLogger;

import in.srain.cube.request.IRequest;
import in.srain.cube.util.CLog;

public class RequestQueue {

    private static final String TAG = "RequestQueue";

    public static boolean DEBUG = false;
    private static RequestQueue sInstance;
    private JobManager mJobManager;

    private RequestQueue() {
    }

    public static synchronized RequestQueue getInstance() {
        if (sInstance == null) {
            sInstance = new RequestQueue();
        }
        return sInstance;
    }

    public synchronized void init(Context context) {

        CustomLogger logger = new CustomLogger() {

            @Override
            public boolean isDebugEnabled() {
                return false;
            }

            @Override
            public void d(String s, Object... objects) {

            }

            @Override
            public void e(Throwable throwable, String s, Object... objects) {

            }

            @Override
            public void e(String s, Object... objects) {

            }

            @Override
            public void v(String s, Object... objects) {

            }
        };

        if (DEBUG) {
            logger = new CustomLogger() {
                private static final String TAG = "JOBS";

                @Override
                public boolean isDebugEnabled() {
                    return false;
                }

                @Override
                public void d(String text, Object... args) {
                    CLog.d(TAG, String.format(text, args));
                }

                @Override
                public void e(Throwable t, String text, Object... args) {
                    CLog.e(TAG, String.format(text, args), t);
                }

                @Override
                public void e(String text, Object... args) {
                    CLog.e(TAG, String.format(text, args));
                }

                @Override
                public void v(String text, Object... args) {
                }
            };
        }
        final Configuration.Builder builder = new Configuration.Builder(context)
                .customLogger(logger)
                .minConsumerCount(3)
                .maxConsumerCount(6)
                .loadFactor(3)
                .consumerKeepAlive(30);

        mJobManager = new JobManager(builder.build());
    }

    public <T, OriginT> RequestJob<T> enqueueRequest(IRequest<T, OriginT> request, int priority) {
        final Params params = new Params(priority)
                .requireNetwork();
        final RequestJob<T> job = new RequestJob<T>(params, request);
        mJobManager.addJobInBackground(job);
        return job;
    }

    public void addJob(Job job) {
        mJobManager.addJobInBackground(job);
    }
}
